﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EntityTest;
using IRepository.Core;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Project.IoC;
using Project.Events;
using Project.Messaging;


using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Net.Http;
using System.Net.Sockets;
using System.Net;


namespace WebTest.Controllers
{
    public class Order_Info
    {
        public Guid OrderID { get; set; }
        public string Title { get; set; }
        public int UserID { get; set; }
        public string UserEmailAddress { get; set; }
    }

    public class OrderSales
    {
        /// <summary>
        /// 生成订单
        /// </summary>
        public void GeneratorOrder(Order_Info entity)
        {
            #region 写订单

            #endregion

            #region 发布写订单事件
            EventBus.Instance.Publish(new OrderGeneratorEvent
            {
                ConfirmedDate = DateTime.Now,
                OrderId = Guid.NewGuid(),
                UserEmailAddress = entity.UserEmailAddress,
                Title = entity.Title,
            });
            EventBus.Instance.UnsubscribeAll<OrderGeneratorEvent>();
            #endregion

        }
    }




    public class HomeController : Controller
    {
        public ActionResult UpLoad()
        {
            return View();
        }
        public ActionResult Index()
        {

            ViewBag.Message = "欢迎使用 ASP.NET MVC!";
            var model = new List<WebManageUsers>();
            //model = ServiceLocator.Instance.GetService<IExtensionRepository<WebManageUsers>>().GetModel().ToList();
            return View(model);
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            #region 事件订阅者
            EventBus.Instance.Subscribe<OrderGeneratorEvent>((args) =>
                MessageFactory.Instance.Send(args.UserEmailAddress
                  , MessageType.Email
                  , args.Title
                  , "订单确认了，订单号" + args.OrderId));

            EventBus.Instance.Subscribe<OrderGeneratorEvent>((args) =>
            {
                //为用户写动态，可能是discus论坛数据
            });
            #endregion

            new OrderSales().GeneratorOrder(new Order_Info
            {
                Title = form["title"],
                UserEmailAddress = form["email"],
                UserID = -1,
            });

            ViewBag.info = "下单成功";
            return View(new List<WebManageUsers>());
        }

        public ActionResult Test()
        {
            List<SelectListItem> temp = new List<SelectListItem>();
            temp.Add(new SelectListItem { Text = "默认值", Value = "" });
            temp.Add(new SelectListItem { Text = "足球", Value = "1" });
            temp.Add(new SelectListItem { Text = "篮球", Value = "2" });

            ViewBag.Temp = temp;
            return View();
        }
        [HttpPost]
        public ActionResult Test(FormCollection form)
        {
            if (ModelState.IsValid)
            {
                //持久化逻辑
            }
            return View();
        }
        public ActionResult About()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult NewWin()
        {
            return View();
        }

        #region 测试异步调用 web api http get 请求
        [HttpGet]
        public void GetApi()
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync("http://localhost:9861/api/Values").Result;
            if (response.IsSuccessStatusCode)
            {
                //response.Content.ReadAsAsync<string>().ContinueWith(i =>
                //{
                //    Console.WriteLine(i.Result);
                //});
                //foreach (var p in products)
                //{
                //    // Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Price, p.Category);
                //}
                this.HttpContext.Response.Write("调用api成功");
            }
            else
            {

                this.HttpContext.Response.Write(string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase));
            }

        }
        #endregion

    }
}
